package com.blensmile.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.blensmile.R;
import com.blensmile.utils.DensityUtils;
import com.blensmile.utils.WaveDampingInterpolator;

/**
 * Created by Blensmile on 2017/2/9.
 */

public class ShakeTip  {

    private PopupWindow popupWindow;
    private Context context;
    private TextView tvText;
    private View llTip;
    private RotateAnimation anim;
    int w ;

    public ShakeTip(Context context,String text) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.tips_layout,null,false);
        tvText = (TextView)view.findViewById(R.id.tv_text);
        tvText.setText(text);
        llTip = view.findViewById(R.id.tips_layout);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,false);
        popupWindow.setAnimationStyle(R.style.ShakeTipSytle);
        anim = new RotateAnimation(7f,-7f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0f);
        anim.setDuration(300);
        anim.setRepeatCount(0);
        anim.setInterpolator(new WaveDampingInterpolator());
        w = DensityUtils.dip2px(context,155);
    }

    public void showAsDropDown(View anchor) {
        int offset = (anchor.getWidth()-w)/2;
        popupWindow.showAsDropDown(anchor,offset,0);
        llTip.startAnimation(anim);
    }

    public void dismiss() {
        llTip.clearAnimation();
        popupWindow.dismiss();
    }

    public boolean isShowing(){
        return  popupWindow.isShowing();
    }
}
